package com.sample.openalpr;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.concurrent.Executor;

public abstract class AsyncTask<Params, Progress, Result> {
    static  final HiLogLabel lable = new HiLogLabel(HiLog.LOG_APP,0x00201,"AsyncTask");
    public final static int POSTEXCEUTE = 1;
    public final static int PROGRESSUPDATE = 2;
    public final static int ONCANCELLED = 3;
    private EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner()) {
        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            switch (event.eventId) {
                case POSTEXCEUTE:
                    Result result = (Result) event.object;
                    onPostExecute(result);
                    break;
                case PROGRESSUPDATE:
                    Progress[] progress = (Progress[]) event.object;
                    onProgressUpdate(progress);
                    break;
                case  ONCANCELLED:
                    Result result2 = (Result) event.object;
                    onCancelled(result2);break;
            }
        }
    };
    private boolean isCancelled = false;

    public void execute(Params... params) {
        onPreExecute();
        new Thread() {
            @Override
            public void run() {
                super.run();
                Result result = doInBackground(params);
                if (isCancelled) {
                    InnerEvent innercancel = InnerEvent.get(ONCANCELLED, result);
                    eventHandler.sendEvent(innercancel);
                } else {
                    InnerEvent innerPostExecute = InnerEvent.get(POSTEXCEUTE, result);
                    eventHandler.sendEvent(innerPostExecute);
                }

            }
        }.start();
    }

    protected void onPreExecute() {
    }

    protected abstract Result doInBackground(Params... params);

    protected void onPostExecute(Result result) {
    }

    protected void onProgressUpdate(Progress... values) {
    }

    public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec, Params... params) {
        onPreExecute();
        Runnable target = new Runnable() {
            @Override
            public void run() {
                Result result = doInBackground(params);
                if (isCancelled) {
                    InnerEvent innercancel = InnerEvent.get(ONCANCELLED, result);
                    eventHandler.sendEvent(innercancel);
                } else {
                    InnerEvent innerPostExecute = InnerEvent.get(POSTEXCEUTE, result);
                    eventHandler.sendEvent(innerPostExecute);
                }
            }
        };
        exec.execute(target);
        return this;
    }

    protected void publishProgress(Progress[] progress) {

        InnerEvent innerProgress = InnerEvent.get(PROGRESSUPDATE, progress);
        eventHandler.sendEvent(innerProgress);
    }

    public void onCancelled(Result result) {
        isCancelled=false;
    }

    public boolean isCancelled() {
        return isCancelled;
    }
    public  void  cancel(boolean b)
    {
        isCancelled=b;
    }
   /*  if (isCancelled()) {
        onCancelled(result);
    } else {
        onPostExecute(result);
    }
    mStatus = Status.FINISHED;*/

}
